home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ov143b.zip / FARMEM.ASM < prev    next >
Assembly Source File  |  1993-01-04  |  4KB  |  138 lines

  1.         PAGE   60,132
  2.         TITLE  Routines allocate/free FAR memory
  3.  
  4. ;  002  23-May-87  farmem.asm
  5.  
  6. ;       Copyright (c) 1986,1987 by Blue Sky Software.  All rights reserved.
  7.  
  8.  
  9. _TEXT   SEGMENT  BYTE PUBLIC 'CODE'
  10. _TEXT   ENDS
  11. _DATA   SEGMENT  WORD PUBLIC 'DATA'
  12. _DATA   ENDS
  13. CONST   SEGMENT  WORD PUBLIC 'CONST'
  14. CONST   ENDS
  15. _BSS    SEGMENT  WORD PUBLIC 'BSS'
  16. _BSS    ENDS
  17. DGROUP  GROUP   CONST,  _BSS,   _DATA
  18.         ASSUME  CS: _TEXT, DS: DGROUP, SS: DGROUP, ES: DGROUP
  19.  
  20. _TEXT   SEGMENT
  21.  
  22. ;****************************************************************************
  23. ;
  24. ;  char far *
  25. ;  malloc_f(size)              /* allocate far memory blk of size bytes */
  26. ;  unsigned int size;          /* not a long */
  27. ;
  28. ;****************************************************************************
  29.  
  30.         PUBLIC  _malloc_f
  31.  
  32. _malloc_f PROC NEAR
  33.         push    bp
  34.         mov     bp,sp
  35.  
  36.         mov     ah,48h                 ;DOS int 21h - 48h is alloc memory
  37.  
  38.         mov     bx,[bp+4]              ;round size up to # paragraphs
  39.         add     bx,15                  ; (size + 15) >> 4
  40.         mov     cl,4
  41.         shr     bx,cl
  42.  
  43.         int     21h                    ;try for it
  44.  
  45.         jc      nomem                  ;carry set if insufficient memory
  46.  
  47.         mov     dx,ax                  ;got it - dx is return segment
  48.         jmp     SHORT mexit
  49.  
  50. nomem:  xor     dx,dx                  ;no mem - return NULL (segment)
  51.  
  52. mexit:  xor     ax,ax                  ;always zero offset
  53.  
  54.         mov     sp,bp
  55.         pop     bp
  56.         ret
  57.  
  58. _malloc_f       ENDP
  59.  
  60.  
  61. ;*****************************************************************************
  62. ;
  63. ;  char far *
  64. ;  largest_f(max,sizep)   /* allocate largest possible block up to max */
  65. ;  unsigned int max, *sizep;
  66. ;
  67. ;*****************************************************************************
  68.  
  69.         PUBLIC  _largest_f
  70.  
  71. _largest_f PROC NEAR
  72.         push    bp
  73.         mov     bp,sp
  74.  
  75. tryall: mov     ah,48h                 ;DOS int 21h - 48h is alloc memory
  76.  
  77.         mov     bx,[bp+4]              ;round size up to # paragraphs
  78.         add     bx,15                  ; (size + 15) >> 4
  79.         mov     cl,4
  80.         shr     bx,cl
  81.  
  82.         int     21h                    ;try for it
  83.  
  84.         jc      nomeml                 ;carry set if insufficient memory
  85.  
  86.         mov     dx,ax                  ;got it - dx is return segment
  87.         mov     bx,[bp+6]              ;return size of block
  88.         mov     ax,[bp+4]
  89.         mov     [bx],ax
  90.  
  91.         jmp     SHORT lexit
  92.  
  93. nomeml: mov     ax,bx
  94.         mov     cl,4                   ;couldn't get it, try what dos said
  95.         shl     ax,cl                  ; was the max (bx)
  96.         mov     [bp+4],ax
  97.         or      ax,ax                  ;shouldn't happen, but...
  98.         jne     tryall
  99.  
  100.         xor     dx,dx                  ;no mem at all! - return NULL (segment)
  101.  
  102. lexit:  xor     ax,ax                  ;always zero offset
  103.  
  104.         mov     sp,bp
  105.         pop     bp
  106.         ret
  107.  
  108. _largest_f      ENDP
  109.  
  110.  
  111. ;*****************************************************************************
  112. ;
  113. ;  free_f(memloc)              /* release far memory */
  114. ;  char far *memloc;
  115. ;
  116. ;*****************************************************************************
  117.  
  118.         PUBLIC _free_f
  119.  
  120. _free_f PROC NEAR
  121.         push    bp
  122.         mov     bp,sp
  123.  
  124.         mov     ax,[bp+6]              ;segmemt of mem to free - offset
  125.         mov     es,ax                  ;  better be 0!
  126.  
  127.         mov     ah,49h                 ;DOS int 21h - 49h releases mem
  128.         int     21h
  129.  
  130.         mov     sp,bp
  131.         pop     bp
  132.         ret
  133.  
  134. _free_f ENDP
  135.  
  136. _TEXT   ENDS
  137. END
  138.